package com.ruoyi.material.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.domain.FoodType;
import com.ruoyi.material.domain.WarehouseStockRecord;
import com.ruoyi.material.mapper.FoodTypeMapper;
import com.ruoyi.material.mapper.WarehouseStockRecordMapper;
import com.ruoyi.material.service.IWarehouseStockRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 仓库库存变动Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class WarehouseStockRecordServiceImpl implements IWarehouseStockRecordService {
    @Resource
    private WarehouseStockRecordMapper warehouseStockRecordMapper;
    @Resource
    private FoodTypeMapper foodTypeMapper;

    /**
     * 查询仓库库存变动
     *
     * @param id 仓库库存变动主键
     * @return 仓库库存变动
     */
    @Override
    public WarehouseStockRecord selectWarehouseStockRecordById(Long id) {
        WarehouseStockRecord warehouseStockRecord = warehouseStockRecordMapper.selectWarehouseStockRecordById(id);
        if (warehouseStockRecord.getfTypeSub() != null) {
            FoodType foodType = foodTypeMapper.selectFoodTypeById(warehouseStockRecord.getfTypeSub());
            if (foodType != null) {
                warehouseStockRecord.setfTypeSubName(foodType.getTypeName());
            }

        }
        warehouseStockRecord.setTypeName(warehouseStockRecord.getType().getTypeName());
        warehouseStockRecord.setfTypeName(warehouseStockRecord.getfType().getTypeName());
        return warehouseStockRecord;
    }

    /**
     * 查询仓库库存变动列表
     *
     * @param warehouseStockRecord 仓库库存变动
     * @return 仓库库存变动
     */
    @Override
    public List<WarehouseStockRecord> selectWarehouseStockRecordList(WarehouseStockRecord warehouseStockRecord) {
        List<WarehouseStockRecord> warehouseStockRecords = warehouseStockRecordMapper.selectWarehouseStockRecordList(warehouseStockRecord);
        warehouseStockRecords.forEach(item -> {
            if (item.getfTypeSub() != null) {
                FoodType foodType = foodTypeMapper.selectFoodTypeById(item.getfTypeSub());
                if (foodType != null) {
                    item.setfTypeSubName(foodType.getTypeName());
                }

            }
            item.setTypeName(item.getType().getTypeName());
            item.setfTypeName(item.getfType().getTypeName());
        });
        return warehouseStockRecords;
    }

    /**
     * 新增仓库库存变动
     *
     * @param warehouseStockRecord 仓库库存变动
     * @return 结果
     */
    @Override
    public int insertWarehouseStockRecord(WarehouseStockRecord warehouseStockRecord) {
        warehouseStockRecord.setCreateTime(DateUtils.getNowDate());
        return warehouseStockRecordMapper.insertWarehouseStockRecord(warehouseStockRecord);
    }

    /**
     * 修改仓库库存变动
     *
     * @param warehouseStockRecord 仓库库存变动
     * @return 结果
     */
    @Override
    public int updateWarehouseStockRecord(WarehouseStockRecord warehouseStockRecord) {
        return warehouseStockRecordMapper.updateWarehouseStockRecord(warehouseStockRecord);
    }

    /**
     * 批量删除仓库库存变动
     *
     * @param ids 需要删除的仓库库存变动主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseStockRecordByIds(Long[] ids) {
        return warehouseStockRecordMapper.deleteWarehouseStockRecordByIds(ids);
    }

    /**
     * 删除仓库库存变动信息
     *
     * @param id 仓库库存变动主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseStockRecordById(Long id) {
        return warehouseStockRecordMapper.deleteWarehouseStockRecordById(id);
    }
}
